package Bytestreamzijie.IOexample;

import java.io.*;

public class IOInputOutByte {

    //字符内存读取数据输入流
    public static void a() throws IOException {
        //读操作
        FileInputStream fileInputStream = new FileInputStream("src/aa.txt");
        int data;
        while ((data = fileInputStream.read()) != -1) {
            System.out.println((char) data);
        }
    }

    //字符向文件中写入数据输出流
    public static void b() throws IOException {
        String data = "WHB,HZW,ZB";
        FileOutputStream fileOutputStream = new FileOutputStream("src/aa.txt");
        fileOutputStream.write(data.getBytes());
    }

    //字节输入读取数据的输入流
    public static void c() {
        byte[] bytes = {100, 99, 45, 55};
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        int data;
        while ((data = byteArrayInputStream.read()) != -1) {
            System.out.println((byte) data);
        }
    }
    //字节数组输出流 将数据输出到字节输出的输出流
    public static void d() throws IOException {
        String data="hello WHB";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(data.getBytes());
        byte[]bytes=byteArrayOutputStream.toByteArray();
        System.out.println("new String(bytes) = " + new String(bytes));
    }
    //object
    //将对象序列化以字节码的形式写入文件   对象-->文件 写操作
    public static void e() throws IOException {
        Class aaa = new Class("aaa", 18);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src/aa.bin"));
        objectOutputStream.writeObject(aaa);
        System.out.println("aaa = " + aaa);
    }

    //object
    //反序列化 读操作
    public static void f() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("src/aa.bin"));
        Class aClass = (Class) objectInputStream.readObject();
        System.out.println("aClass = " + aClass);
    }
    //data 读取
    public static void g() throws IOException {
        DataInputStream dataInputStream = new DataInputStream(new FileInputStream("src/data.bin"));
        int i = dataInputStream.readInt();
        double v = dataInputStream.readDouble();
        System.out.println("i = " + i);
        System.out.println("v = " + v);

    }
    //data写入
    public static void h() throws IOException {
        DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("src/data.bin"));
        int aa=98;
        Double bb=12.5;
        dataOutputStream.writeInt(aa);
        dataOutputStream.writeDouble(bb);
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        a();
//        b();
//        c();
//        d();
//        e();
//        f();
        g();
//        h();
    }
}
